/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.lcpgenerator.method;

import com.inspur.edp.bef.api.be.BEInfo;
import com.inspur.edp.bef.dtgenerator.core.rtgen.lcpgenerator.LcpClassGeneratorContext;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class LcpBeInfoGenerator extends ClassMethodGenerator {
	private LcpClassGeneratorContext lcpGenContext;
	private String beInfoPropName = "info";


	public static LcpBeInfoGenerator Create(LcpClassGeneratorContext lcpGenContext) {
		return new LcpBeInfoGenerator(lcpGenContext);
	}

	private LcpBeInfoGenerator(LcpClassGeneratorContext lcpContext) {
		lcpGenContext = lcpContext;
	}

	@Override
	protected boolean getIsOverride() {
		return true;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		return new ArrayList<Modifier.ModifierKeyword>(java.util.Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PROTECTED_KEYWORD}));
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		return null;
	}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(BEInfo.class);
	}

	@Override
	protected String getMethodName() {
		return "innerGetBEInfo";
	}

	@Override
	protected Block buildMethodBody() {
		Block block = ast.newBlock();

		// BEInfo info=new BEInfo();
		buildNewBEInfo(block);
		// info.setBEType(XX);
		buildSetBEType(block);
		// info.setBEID(XX);
		buildSetBEId(block);

		// return info;
		ReturnStatement returnStatement = ast.newReturnStatement();
		SimpleName infoProp=ast.newSimpleName(beInfoPropName);
		returnStatement.setExpression(infoProp);
		block.statements().add(returnStatement);
		return block;
	}

	/**
	 * info.setBEId();
	 *
	 * @param block
	 */
	private void buildSetBEId(Block block) {
		String beId = lcpGenContext.getProjContext().getBE().getID();
		if (beId == null) {
			beId = "";
		}
		block.statements().add(ast.newExpressionStatement(buildSetValueMethodInvocation(beInfoPropName, "setBEID",
				beId)));
	}

	/**
	 * info.setBEType();
	 *
	 * @param block
	 */
	private void buildSetBEType(Block block) {
		String beType = lcpGenContext.getProjContext().getBE().getGeneratedConfigID();
		block.statements().add(ast.newExpressionStatement(buildSetValueMethodInvocation(beInfoPropName, "setBEType",
				beType)));
	}

	private MethodInvocation buildSetValueMethodInvocation(String varName, String setMethodName, String propValue) {
		MethodInvocation setValueMethodInvocation = ast.newMethodInvocation();
		setValueMethodInvocation.setExpression(ast.newSimpleName(varName));
		setValueMethodInvocation.setName(ast.newSimpleName(setMethodName));

		StringLiteral propValueLiteral = ast.newStringLiteral();
		propValueLiteral.setLiteralValue(propValue);
		setValueMethodInvocation.arguments().add(propValueLiteral);
		return setValueMethodInvocation;
	}

	/**
	 * BEInfo beInfo=new BEInfo();
	 *
	 * @param block
	 */
	private void buildNewBEInfo(Block block) {
		VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();

		variableDeclarationFragment.setName(ast.newSimpleName(beInfoPropName));
		ClassInstanceCreation beInfoInstanceCreation = ast.newClassInstanceCreation();
		beInfoInstanceCreation.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(BEInfo.class).getType(ast,
				lcpGenContext.getProjContext()));
		variableDeclarationFragment.setInitializer(beInfoInstanceCreation);

		VariableDeclarationStatement variableDeclarationStatement = ast.newVariableDeclarationStatement(variableDeclarationFragment);
		variableDeclarationStatement.setType(new com.inspur.edp.caf.generator.baseInfo.TypeInfo(BEInfo.class).getType(ast,
				lcpGenContext.getProjContext()));
		block.statements().add(variableDeclarationStatement);
	}
}
